class Solution {
   public:
    int removeDuplicates(vector<int>& nums) {
        int n = nums.size(), l = 0, r = 0;
        if (n == 0) return 0;
        while (r < n) {
            while (r < n && nums[r] == nums[l]) r++;
            if (r < n) nums[++l] = nums[r++];
        }
        return l + 1;
    }
};